package cn.wolfcode.wolf2w.util;

import cn.wolfcode.wolf2w.query.QueryObject;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * 数据库的工具类
 */
public abstract class DBHelper {

    /**
     * 分页查询工具方法
     * @param template
     * @param query
     * @param clz
     * @param <T>
     * @return
     */
    public static <T> Page<T> query(MongoTemplate template, Pageable pageable, Query query, Class<T> clz){
        // 查询总数
        long count = template.count(query, clz);
        if (count == 0) {
            return Page.empty();
        }
        query.with(pageable);
        List<T> data = template.find(query, clz);
        return new PageImpl<>(data, pageable, count);
    }
}
